﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eShop.Business;
using eShop.DataAccess;
using eShop.Business;
using eShop.Model;

public partial class admin_product_review_edit : SiteAdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadReview();
        }
    }
    private void LoadReview()
    {
        try
        {
            ProductReview review = ProductManager.GetTblProductReviewByProductReviewId(ReviewID);
            if (review != null)
            {
                txtComment.Text = Server.HtmlDecode(review.ReviewText).Replace("<br />", "\n");
                txtCustomerEmail.Text = review.CustomerEmail;
                txtCustomerName.Text = review.CustomerName;
                txtProductName.Text = review.ProductName;
                hidRate.Value= review.RatingValue.Value.ToString();
                chkApproved.Checked = review.IsApproved.Value;
            }
        }
        catch (Exception)
        {
            uccResponseMessage.SetFailMessage("Error Loading Review");
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            ProductReview review = ProductManager.GetTblProductReviewByProductReviewId(ReviewID);
            if (review != null)
            {
                review.CustomerName = txtCustomerName.Text;
                review.CustomerEmail = txtCustomerEmail.Text;
                review.ReviewText = Server.HtmlEncode(txtComment.Text.Replace("\n", "<br />"));
                review.RatingValue = Convert.ToDecimal(hidRate.Value.Trim()); ;
                review.IsApproved = chkApproved.Checked;
                ProductManager.SaveProductReview(review);

                uccResponseMessage.SetSuccessMessage("Review Updated Successfully");

                new Email().SendGenericEmail(review.CustomerName, review.CustomerEmail, "Review Status Updated", String.Format("Your product review for the product <strong>{0}</strong> has been {1} by the administrator.", review.ProductName, review.IsApproved.Value ? "published" : "declined"));
            }
        }
        catch (Exception ex)
        {
            //Response.Write(ex.Message);
            uccResponseMessage.SetFailMessage("An unknown error occured while updating the review.");
        }
    }
    
    private int ReviewID
    {
        get
        {
            int reviewId = 0;
            if (Request["rid"] != null && !String.IsNullOrEmpty(Request["rid"]))
                Int32.TryParse(Request["rid"].ToString(), out reviewId);
            return reviewId;
        }
    }
}